package edu.uj.petri.view;

import edu.uj.petri.commands.DeletePointCommand;
import edu.uj.petri.commands.Memento;
import edu.uj.petri.document.Document;
import java.awt.geom.Point2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

public class LinkingPoint extends DiagramElement {
    
    public Point2D position;
    
    /**
     * size of square when linking point appears as a square
     */
    private static double size = 5.0;
    
    public LinkingPoint(Point2D position) {
        
        this.position = position;
    }
    
    /**
     * check if point is in square
     */
    private Rectangle2D getElementRect() {
        return new Rectangle2D.Double(
                position.getX() - size,
                position.getY() - size,
                2*size, 2*size );
    }
    
    public boolean containsPoint(Point2D point) {
        
        return (new Rectangle2D.Double(
                position.getX() - 2*size,
                position.getY() - 2*size,
                4*size, 4*size )).contains( point );
    }
    
    public boolean crossesRectangle(Rectangle2D rect) {
        return false;
    }
    
    /**
     * draw as square if selected, otherwise don't draw
     */
    public void draw(Canvas canvas) {
        
        if (selected)
            canvas.drawRectangle(getElementRect(), SELECTED_COLOR, false);
    }
    
    /**
     * return containPoint()
     */
    public boolean canBeMoved(Point2D startPoint) {
        
        return containsPoint(startPoint);
    }
    
    public void move(Point2D start, Point2D end) {
        
        Point2D difference = new Point2D.Double( start.getX() - end.getX(), start.getY() - end.getY());
        position.setLocation( position.getX() + difference.getX(), position.getY() + difference.getY());
    }
    
    public void doubleClick( Point2D point ) {
    }
    
    /**
     * entries: remove point
     */
    public ContextMenu getContextMenu( ArcView arcView) {
        
        ContextMenu menu = new ContextMenu();
        
        menu.addItem("Remove points", new DeletePointCommand(arcView, this));
        
        return menu;
    }
    
    public void removeFrom(View view) {
    }
    
    public ContextMenu getContextMenu() {
        return null;
    }
    
    public Memento getMemento() {
        LinkingPointMemento retval = new LinkingPointMemento();
        retval.position = (Point2D)position.clone();
        return retval;
    }
    
    public void applyMemento(Memento memento) {
        LinkingPointMemento myMemento = (LinkingPointMemento)memento;
        position = (Point2D)myMemento.position.clone();
    }
    
    static class LinkingPointMemento extends Memento {
        private Point2D position;
    }
    
    private static final long serialVersionUID = -2293327390244863560L;
}
